﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYX.Print.Models.Models;
using XYX.Print.Utils;
using Dapper;

namespace XYX.Print.Impl
{
    /// <summary>
    /// 配置
    /// </summary>
    public class SysRegistConfigImpl
    {
     
        /// <summary>
        /// 加盐
        /// </summary>
        private string key = "xialei";
        /// <summary>
        /// 获取配置
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        /// <exception cref="BusinessException"></exception>
        public async Task<SysRegistConfig> Get(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                throw new BusinessException("编码不能空空");
            }
            using (IDbConnection conn = new SqlConnection(DbConnectionManager.ConnString))
            {
                conn.Open();
                string sql = "SELECT  TOP 1 * FROM  SysRegistConfig WHERE Code=@Code";
                SysRegistConfig sysConfig = await conn.QueryFirstOrDefaultAsync<SysRegistConfig>(sql, new { Code = code });

                return sysConfig;
            }
        }

        /// <summary>
        /// 获取配置
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        /// <exception cref="BusinessException"></exception>
        public async Task<bool> CheckIsRegisted(string cpu)
        {

            using (IDbConnection conn = new SqlConnection(DbConnectionManager.ConnString))
            {
                conn.Open();
                string sql = "SELECT  TOP 1 * FROM  SysRegistConfig WHERE Code=@Code";
                SysRegistConfig sysConfig = await conn.QueryFirstOrDefaultAsync<SysRegistConfig>(sql, new { Code = cpu });
                if (sysConfig == null)
                {
                    return false;
                }
                if (sysConfig.Value == SecurityHelper.MD5($"{key}{cpu}"))
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="cpu"></param>
        /// <returns></returns>
        public async Task<bool> Register(string cpu)
        {
            using (IDbConnection conn = new SqlConnection(DbConnectionManager.ConnString))
            {
                conn.Open();
                string tCpu = SecurityHelper.MD5($"{key}{cpu}");
                SysRegistConfig oldSysConfig = await Get(cpu);
                if (oldSysConfig == null)
                {
                    SysRegistConfig sysConfig = new SysRegistConfig()
                    {
                        Code = cpu,
                        CreateDateTime = DateTime.Now,
                        CreateUserName = "管理员",
                        CreateUserId = "1",
                        IsDeleted = false,
                        Value = tCpu,
                        ExpEndDateTime = DateTime.Now.AddYears(10),
                        ExpStartDateTime = DateTime.Now,
                    };
                    await conn.InsertAsync(sysConfig);
                }
                else
                {
                    oldSysConfig.UpdateDateTime = DateTime.Now;
                    oldSysConfig.UpdateUserId = "1";
                    oldSysConfig.UpdateUserName = "管理员";
                    conn.Update(oldSysConfig);
                }
                return true;

            }

        }
        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="sysConfig"></param>
        /// <returns></returns>
        /// <exception cref="BusinessException"></exception>
        public async Task<SysRegistConfig> Insert(SysRegistConfig sysConfig)
        {

            using (IDbConnection conn = new SqlConnection(DbConnectionManager.ConnString))
            {
                conn.Open();
                await conn.InsertAsync(sysConfig);
                return sysConfig;
            }
        }
    }
}
